#ifndef _TRANSLATION_MANAGER_H_
#define _TRANSLATION_MANAGER_H_

#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <filesystem>
#include "cJSON.h"

// 定义结构体来保存文件路径和分割后的文本内容
struct FileContent {
    std::string path;
    std::vector<std::string> content;
};

class TranslationManager {
private:
public:
    int paragraph_effective = 512; // 段落有效值
    int paragraph_min = 512;       // 段落最小值(根据n_batch计算)
    int paragraph_max = 1024;      // 段落最大值(根据n_batch计算)

    const char *chatPrefix = "Original text to be translated:\n"; // 翻译前缀

    // 输出文件路径 翻译途中双语对比
    std::string output_comparison_file_path = "./examples/fxxk_datasheet_llama_cpp/file_processing/datasheet_output/output_comparison.md";

    std::filesystem::path directory = "./examples/fxxk_datasheet_llama_cpp/file_processing/datasheet_input/";              // 原输入路径
    std::filesystem::path directory_cut = "./examples/fxxk_datasheet_llama_cpp/file_processing/datasheet_output/docs_cut"; // 切割输出路径
    std::filesystem::path directory_en = "./examples/fxxk_datasheet_llama_cpp/file_processing/datasheet_output/docs_en";   // 翻译输出路径

    std::string progress_file = "./examples/fxxk_datasheet_llama_cpp/file_processing/datasheet_output/progress.json"; // 进度文件

    std::vector<FileContent> translation_cache; // 所有文件待翻译的内容

    std::ostringstream my_output_ss;

    size_t file_index = 0;      // 文件计数
    size_t paragraph_index = 0; // 段落计数

    unsigned int error_count = 0;
    unsigned int restart_count = 0; // 重启计数 避免多次运行后有脏回答污染后续翻译

    TranslationManager();  // 构造函数
    ~TranslationManager(); // 析构函数

    void removeTrailingImEndTag(std::string &str);
    bool isTitle(const std::string &line);
    void ProcessFilesCut(const std::filesystem::path &directory, const std::filesystem::path &target_directory, const std::filesystem::path &root_directory);
    FileContent getFileContent(const std::filesystem::directory_entry &entry);
    void ProcessFilesRecursive(const std::filesystem::path &directory, const std::filesystem::path &target_directory, const std::filesystem::path &root_directory, std::vector<FileContent> &result);
    void ReadTranslated(const std::filesystem::path &directory, const std::filesystem::path &root_directory, std::vector<FileContent> &result);
    void SaveToFilesystem(const std::vector<FileContent> &result, const std::filesystem::path &out_root_directory);
    bool fileExists(const std::string &filename);
    void writeJsonToFile(const std::string &filename, size_t file_index, size_t paragraph_index);
    bool readJsonFromFile(const std::string &filename, size_t &file_index, size_t &paragraph_index);
};

#endif // TRANSLATIONMANAGER_H
